package com.example.keli.ui.list;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.keli.R;
import com.example.keli.base.utils.GlideUtil;
import com.example.keli.base.utils.LogUtils;
import com.example.keli.ui.home.activity.ObjectDetailActivity;

import java.util.List;


public class RankItemLayout extends LinearLayout {
    private int defaultColor = 0x20000000;
    private Context mContext;
    private View mView;
    private Drawable mTopEndDrawable;
    private CharSequence mTopTextString;
    private CharSequence mBotTextString;
    private int mTopTextSize;
    private int mBotTextSize;
    private TextView mIconTitle;
    private ImageView mIcon;
    private TextView mMore;
    private LinearLayout container;
    private HorizontalScrollView mScrollView;
    private boolean mShowMark = true;
    private boolean mShowTopMark = false;

    private int mTopColor;
    private int mBotColor;

    public RankItemLayout(Context context) {
        this(context, null);
    }

    public RankItemLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        mView = View.inflate(context, R.layout.diy_rank_layout, this);
        mIcon = (ImageView) mView.findViewById(R.id.icon);
        mIconTitle = (TextView) mView.findViewById(R.id.icon_title);
        mScrollView = (HorizontalScrollView) mView.findViewById(R.id.h_scrollview);
        mMore = (TextView) mView.findViewById(R.id.more);
        container = (LinearLayout) mView.findViewById(R.id.container);
        getAttr(attrs);
    }

    private void getAttr(AttributeSet attrs) {
//        TypedArray typedArray = mContext.obtainStyledAttributes(attrs, R.styleable.TxtLinearLayout);
//        mTopEndDrawable = typedArray.getDrawable(R.styleable.TxtLinearLayout_top_end_icon);
//        mTopTextString = typedArray.getString(R.styleable.TxtLinearLayout_top_text);
//        mBotTextString = typedArray.getString(R.styleable.TxtLinearLayout_bot_text);
//        mTopTextSize = typedArray.getDimensionPixelSize(R.styleable.TxtLinearLayout_top_text_size, 13);
//        mBotTextSize = typedArray.getDimensionPixelSize(R.styleable.TxtLinearLayout_bot_text_size, 13);
//        mTopColor = typedArray.getColor(R.styleable.TxtLinearLayout_top_text_color, defaultColor);
//        mBotColor = typedArray.getColor(R.styleable.TxtLinearLayout_bot_text_color, defaultColor);
//        mShowMark = typedArray.getBoolean(R.styleable.TxtLinearLayout_show_mark, true);
//        mShowTopMark = typedArray.getBoolean(R.styleable.TxtLinearLayout_show_top_mark, false);
//        mDesTv.setTextSize(TypedValue.COMPLEX_UNIT_PX, mTopTextSize);
//        mDesTv.setText(mTopTextString);
//        mNumTv.setTextSize(TypedValue.COMPLEX_UNIT_PX, mBotTextSize);
//        mNumTv.setText(mBotTextString);
//        mDesTv.setTextColor(mTopColor);
//        mNumTv.setTextColor(mBotColor);
//        if (null != mTopEndDrawable) {
////            mTopEndDrawable.setBounds(0, 0, DisplayUtil.dip2px(20), DisplayUtil.dip2px(20));
////            mNumTv.setCompoundDrawables(null,null,mTopEndDrawable,null);
//            mMark.setImageDrawable(mTopEndDrawable);
//        }
//        if (mShowMark) {
//            mMark.setVisibility(VISIBLE);
//        } else {
//            mMark.setVisibility(GONE);
//        }
//        if(mShowTopMark){
//            mTopMark.setVisibility(VISIBLE);
//        }else{
//            mTopMark.setVisibility(GONE);
//        }
//        typedArray.recycle();
    }

    TextView ticketNum;
    TextView nickName;
    ImageView avatar;
    View mItemView;

    public void setTicketNum(final List<RankData.DataBean.TicketUserBean> ticketUserBean) {
        container.removeAllViews();
        if(ticketUserBean.size()==0){
            mMore.setVisibility(View.GONE);
            mItemView = View.inflate(mContext,R.layout.diy_rank_empty_text_layout,null);
            TextView textView  =mItemView.findViewById(R.id.empty);
            textView.setText("暂时还没人投票哦，期待你的助力");
            container.addView(mItemView);
            return;
        }
        mMore.setVisibility(View.VISIBLE);
        for (int i = 0; i < ticketUserBean.size(); i++) {
            mItemView = View.inflate(mContext, R.layout.diy_rank_img_text_layout, null);
            ticketNum = mItemView.findViewById(R.id.ticket_num);
            avatar = mItemView.findViewById(R.id.avatar);
            nickName = mItemView.findViewById(R.id.nick_name);
            GlideUtil.displayCircle(mContext, avatar, ticketUserBean.get(i).getAvatar());
            ticketNum.setText(ticketUserBean.get(i).getTicket_num() + "次助力");
            nickName.setText(ticketUserBean.get(i).getNickname());
            final String uid = String.valueOf(ticketUserBean.get(i).getUid());
            final String aaid = String.valueOf(ticketUserBean.get(i).getAaid());
            mItemView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    LogUtils.loge("这个ID是多少>>>"+uid+"卧槽"+aaid);
                    ObjectDetailActivity.start(mContext, uid, aaid);
                }
            });
            container.addView(mItemView);
            if (i == 3) {
                break;
            }
        }
    }

    public void setMore(final String id, final int flag){
        mMore.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                //flag 0是助力榜，1是粉丝榜
                if(flag==0){
                    //助力榜
                    TicketListActivity.start(mContext,id,0);
                }else{
                    //粉丝榜
                    TicketListActivity.start(mContext,id,1);
                }
            }
        });
    }

    public void setGiftNum(List<RankData.DataBean.GiftUserBean> giftUserBeans) {
        container.removeAllViews();
        if(giftUserBeans.size()==0){
            mMore.setVisibility(View.GONE);
            mItemView = View.inflate(mContext,R.layout.diy_rank_empty_text_layout,null);
            TextView textView  =mItemView.findViewById(R.id.empty);
            textView.setText("暂时还没人送礼哦，期待你的礼物");
            container.addView(mItemView);
            return;
        }
        mMore.setVisibility(View.VISIBLE);
        for (int i = 0; i < giftUserBeans.size(); i++) {
            mItemView = View.inflate(mContext, R.layout.diy_rank_img_text_layout, null);
            ticketNum = mItemView.findViewById(R.id.ticket_num);
            avatar = mItemView.findViewById(R.id.avatar);
            nickName = mItemView.findViewById(R.id.nick_name);
            GlideUtil.displayCircle(mContext, avatar, giftUserBeans.get(i).getAvatar());
            ticketNum.setText(giftUserBeans.get(i).getLike_num() + "份");
            nickName.setText(giftUserBeans.get(i).getNickname());
            final String uid = String.valueOf(giftUserBeans.get(i).getUid());
            final String aaid = String.valueOf(giftUserBeans.get(i).getAaid());

            mItemView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    LogUtils.loge("这个ID是多少>>>"+uid+"卧槽"+aaid);
                    ObjectDetailActivity.start(mContext, uid, aaid);
                }
            });
            container.addView(mItemView);
            if (i == 3) {
                break;
            }
        }
    }

    public void setIcon(int resource) {
        mIcon.setImageResource(resource);
    }

    public void setIconTitle(String resource) {
        mIconTitle.setText(resource);
    }
}